﻿using System.Diagnostics;
using System.Reflection;
using Abp.AspNetCore.Configuration;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Modules;
using Abp.Runtime.Session;
using Magicodes.WeChat.Authorization;
using Magicodes.WeChat.Configuration;
using Magicodes.WeChat.Core;
using Magicodes.WeChat.SDK;
using Magicodes.WeChat.SDK.Builder;
using Magicodes.WeChat.WeChatApiSetting.Dto;
using Newtonsoft.Json;
using System;
using Magicodes.WeChat.Application.Startup;
using Abp.Runtime.Caching;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
#if !FEATURE_OLD
using Magicodes.Admin.Configuration;
using Magicodes.Admin;
#endif


namespace Magicodes.WeChat.Application
{
    [DependsOn(
        typeof(WeChatCoreModule),
#if !FEATURE_OLD
typeof(AdminApplicationModule),
#endif

        typeof(AbpAutoMapperModule))]
    public class WeChatApplicationModule : AbpModule
    {
        public override void PreInitialize()
        {
            //添加权限程序
            Configuration.Authorization.Providers.Add<WeChatAuthorizationProvider>();

            //配置指定的Cache过期时间
            Configuration.Caching.Configure("WeChatSetting", cache =>
            {
                cache.DefaultSlidingExpireTime = TimeSpan.FromHours(2);
            });
        }

        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
            var cacheManager = IocManager.Resolve<ICacheManager>();
#if !FEATURE_OLD
            var appConfiguration = IocManager.Resolve<IAppConfigurationAccessor>()?.Configuration;
            //配置微信SDK
            WeChatSDKConfigHelper.Config(Logger, cacheManager, appConfiguration, IocManager);
#else
            WeChatSDKConfigHelper.Config(Logger, cacheManager, null, IocManager);
#endif

        }
    }
}